package com.gcpproxy.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * 应用配置类
 * 统一管理应用的所有配置
 */
@Configuration
@ConfigurationProperties(prefix = "app")
@Validated
public class AppConfig {

    /**
     * 服务器配置
     */
    private Server server = new Server();

    /**
     * Google Cloud配置
     */
    private GoogleCloud googleCloud = new GoogleCloud();

    /**
     * 认证配置
     */
    private Auth auth = new Auth();

    /**
     * 图片处理配置
     */
    private Image image = new Image();

    /**
     * 日志配置
     */
    private Logging logging = new Logging();

    /**
     * 代理配置
     */
    private Proxy proxy = new Proxy();

    // Getters and Setters
    public Server getServer() {
        return server;
    }

    public void setServer(Server server) {
        this.server = server;
    }

    public GoogleCloud getGoogleCloud() {
        return googleCloud;
    }

    public void setGoogleCloud(GoogleCloud googleCloud) {
        this.googleCloud = googleCloud;
    }

    public Auth getAuth() {
        return auth;
    }

    public void setAuth(Auth auth) {
        this.auth = auth;
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public Logging getLogging() {
        return logging;
    }

    public void setLogging(Logging logging) {
        this.logging = logging;
    }

    public Proxy getProxy() {
        return proxy;
    }

    public void setProxy(Proxy proxy) {
        this.proxy = proxy;
    }

    /**
     * 服务器配置
     */
    public static class Server {
        @NotBlank
        private String host = "0.0.0.0";

        @Positive
        private int port = 5019;

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }
    }

    /**
     * Google Cloud配置
     */
    public static class GoogleCloud {
        @NotBlank
        private String project = "gcpproject-25sep08";

        @NotBlank
        private String location = "global";

        private String credentials;

        public String getProject() {
            return project;
        }

        public void setProject(String project) {
            this.project = project;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public String getCredentials() {
            return credentials;
        }

        public void setCredentials(String credentials) {
            this.credentials = credentials;
        }
    }

    /**
     * 认证配置
     */
    public static class Auth {
        private boolean enabled = true;

        /**
         * 有效的Bearer Token列表
         */
        private List<String> validTokens = new ArrayList<>();

        /**
         * 管理员专用token
         */
        private String adminToken = "";

        @NotBlank
        private String jwtSecretKey = "your-secret-key-change-this-in-production";

        @Positive
        private int accessTokenExpireMinutes = 1440;

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public String getJwtSecretKey() {
            return jwtSecretKey;
        }

        public void setJwtSecretKey(String jwtSecretKey) {
            this.jwtSecretKey = jwtSecretKey;
        }

        public int getAccessTokenExpireMinutes() {
            return accessTokenExpireMinutes;
        }

        public void setAccessTokenExpireMinutes(int accessTokenExpireMinutes) {
            this.accessTokenExpireMinutes = accessTokenExpireMinutes;
        }

        public List<String> getValidTokens() {
            return validTokens;
        }

        public void setValidTokens(List<String> validTokens) {
            this.validTokens = validTokens != null ? validTokens : new ArrayList<>();
        }

        public String getAdminToken() {
            return adminToken;
        }

        public void setAdminToken(String adminToken) {
            this.adminToken = adminToken;
        }

        /**
         * 检查token是否有效
         * @param token 要验证的token
         * @return true如果token有效，false否则
         */
        public boolean isValidToken(String token) {
            if (token == null || token.trim().isEmpty()) {
                return false;
            }
            
            // 检查管理员token
            if (StringUtils.hasText(adminToken) && adminToken.equals(token)) {
                return true;
            }
            
            // 检查普通token列表
            return validTokens != null && validTokens.contains(token);
        }
    }

    /**
     * 图片处理配置
     */
    public static class Image {
        @NotBlank
        private String logPath = "/logs/gcp-proxy/images";

        @Positive
        private long maxSize = 50 * 1024 * 1024; // 50MB

        public String getLogPath() {
            return logPath;
        }

        public void setLogPath(String logPath) {
            this.logPath = logPath;
        }

        public long getMaxSize() {
            return maxSize;
        }

        public void setMaxSize(long maxSize) {
            this.maxSize = maxSize;
        }
    }

    /**
     * 日志配置
     */
    public static class Logging {
        @NotBlank
        private String level = "INFO";

        @NotBlank
        private String file = "/logs/gcp-proxy/log.txt";

        public String getLevel() {
            return level;
        }

        public void setLevel(String level) {
            this.level = level;
        }

        public String getFile() {
            return file;
        }

        public void setFile(String file) {
            this.file = file;
        }
    }

    /**
     * 代理配置
     */
    public static class Proxy {
        private boolean enabled = true;

        @NotBlank
        private String host = "127.0.0.1";

        @Positive
        private int port = 7897;

        private int connectTimeout = 30000;

        private int responseTimeout = 60000;

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }

        public int getConnectTimeout() {
            return connectTimeout;
        }

        public void setConnectTimeout(int connectTimeout) {
            this.connectTimeout = connectTimeout;
        }

        public int getResponseTimeout() {
            return responseTimeout;
        }

        public void setResponseTimeout(int responseTimeout) {
            this.responseTimeout = responseTimeout;
        }
    }
}